﻿using System;
using System.Data;
using System.IO;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;

namespace HRM
{
    public partial class frmEmployeeNotActiveList : DockContent
    {
        DateTime _DOB = DateTime.MinValue;

        EmployeeClient wcfEmployee;
        BranchClient wcfBranch;
        DepartmentClient wcfDepartment;
        RoleRightInfo listRole;
        MainServiceClient wcfMain;
        DetailClient _wcfDetail;
        int itemCount = 0;
        int pageSize = UIParamater.pageSize;
        int totalPage = 0;
        int currPage = 0;
        string _OrderBy = "";
        string _OrderDirection = "";
        int employeeID = 0;
        int branch_ID = 0;
        int department_ID = 0;
        DataSet datasetForExcel = null;
        public frmEmployeeNotActiveList()
        {
            InitializeComponent();
            //LoadLanguage();
            wcfEmployee = Service.Employee();
            wcfBranch = Service.Branch();
            wcfDepartment = Service.Department();
            wcfMain = Service.MainService();
            UISetting.LoadTreeView3(ref tvBranch);
            listRole = UIParamater.Quyen_HoSoCanBo;
            _wcfDetail = Service.Detail();

            if (!listRole.Update)
            {
                this.btnEdit.Image = global::HRM.Properties.Resources.app;
                this.btnEdit.Tag = "xem";
            }
            BuildVoucherGrid();
            LoadData();
        }

        private void LoadLanguage()
        {
            this.TabText = this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_ContextMenuStrip(ref mnuPopUp);
        }


        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_ID", "Mã chính", "", 1, false, true);
            UISetting.AddColumn(ref dgvEmployeeList, "RowNumber", UpdateUI.GetString("stt"), "C", 40, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_Code", UpdateUI.GetString("maso"), "", 120, false, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_SIBookNo", "Số sổ BH", "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_FullName", UpdateUI.GetString("hovaten"), "", 150, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_Sex", "", "C", 50, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_DateOfBirth", UpdateUI.GetString("ngaysinh"), "C", 105, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_PlaceOfBirth", UpdateUI.GetString("noisinh"), "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_Address", UpdateUI.GetString("diachi"), "", 150, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_Phone", UpdateUI.GetString("dienthoai"), "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "MaritalStatus_Name", UpdateUI.GetString("tinhtranghonnhan"), "", 150, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Religion_Name", UpdateUI.GetString("tongiao"), "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Ethnic_Name", UpdateUI.GetString("dantoc"), "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_IDCardNo", UpdateUI.GetString("CMTND"), "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_IDCardIssueDate", UpdateUI.GetString("ngaycap"), "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_IDCardIssueAuthority", UpdateUI.GetString("noicap"), "", 150, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_BankAccountNo", UpdateUI.GetString("sotaikhoan"), "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_BankName", UpdateUI.GetString("nganhang"), "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_StartDate", UpdateUI.GetString("ngaybatdau"), "", 120, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_EndDate", UpdateUI.GetString("ngayketthuc"), "", 120, true, true);
            //UISetting.AddColumn(ref dgvEmployeeList, "Branch_Name", "Chi nhánh", "", 100, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Department_Name", "Phòng ban", "", 130, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Position_Name", "Chức vụ", "", 110, true, true);
        }

        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }

        private void LoadData()
        {
            DataSet ds = wcfEmployee.Employee_GetListNotActive(out itemCount, UISetting.GetKeyWord(txtKeyWord.Text.Trim()), _OrderBy, _OrderDirection, currPage, pageSize, branch_ID, department_ID);
            datasetForExcel = wcfEmployee.Employee_GetListNotActive(out itemCount, UISetting.GetKeyWord(txtKeyWord.Text.Trim()), _OrderBy, _OrderDirection, currPage, itemCount, branch_ID, department_ID);
            PageIndex();
            dgvEmployeeList.Rows.Clear();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dgvEmployeeList.Rows.Insert(i, 1);
                dgvEmployeeList.Columns["Employee_FullName"].Frozen = true;
                dgvEmployeeList.Rows[i].Cells["RowNumber"].Value = ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_ID"].Value = ds.Tables[0].Rows[i]["Employee_ID"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_Code"].Value = ds.Tables[0].Rows[i]["Employee_Code"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_SIBookno"].Value = ds.Tables[0].Rows[i]["Employee_SIBookno"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_FullName"].Value = ds.Tables[0].Rows[i]["Employee_FullName"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_Sex"].Value = ds.Tables[0].Rows[i]["Employee_Sex"].ToString();
                if (ds.Tables[0].Rows[i]["Employee_DateOfBirth"] != DBNull.Value)
                    dgvEmployeeList.Rows[i].Cells["Employee_DateOfBirth"].Value = Convert.ToDateTime(ds.Tables[0].Rows[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy");
                else
                    dgvEmployeeList.Rows[i].Cells["Employee_DateOfBirth"].Value = "";
                dgvEmployeeList.Rows[i].Cells["Employee_PlaceOfBirth"].Value = ds.Tables[0].Rows[i]["Employee_PlaceOfBirth"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_Address"].Value = ds.Tables[0].Rows[i]["Employee_Address"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_Phone"].Value = ds.Tables[0].Rows[i]["Employee_Phone"].ToString();
                dgvEmployeeList.Rows[i].Cells["MaritalStatus_Name"].Value = ds.Tables[0].Rows[i]["MaritalStatus_Name"].ToString();
                dgvEmployeeList.Rows[i].Cells["Religion_Name"].Value = ds.Tables[0].Rows[i]["Religion_Name"].ToString();
                dgvEmployeeList.Rows[i].Cells["Ethnic_Name"].Value = ds.Tables[0].Rows[i]["Ethnic_Name"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_IDCardNo"].Value = ds.Tables[0].Rows[i]["Employee_IDCardNo"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_IDCardIssueDate"].Value
                    = ds.Tables[0].Rows[i]["Employee_IDCardIssueDate"] == DBNull.Value ? ""
                    : Convert.ToDateTime(ds.Tables[0].Rows[i]["Employee_IDCardIssueDate"]).ToString("dd/MM/yyyy");
                dgvEmployeeList.Rows[i].Cells["Employee_IDCardIssueAuthority"].Value = ds.Tables[0].Rows[i]["Employee_IDCardIssueAuthority"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_BankAccountNo"].Value = ds.Tables[0].Rows[i]["Employee_BankAccountNo"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_BankName"].Value = ds.Tables[0].Rows[i]["Employee_BankName"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_StartDate"].Value
                    = ds.Tables[0].Rows[i]["Employee_StartDate"] == DBNull.Value ? ""
                    : Convert.ToDateTime(ds.Tables[0].Rows[i]["Employee_StartDate"]).ToString("dd/MM/yyyy");
                dgvEmployeeList.Rows[i].Cells["Employee_EndDate"].Value
                    = ds.Tables[0].Rows[i]["Employee_EndDate"] == DBNull.Value ? ""
                    : Convert.ToDateTime(ds.Tables[0].Rows[i]["Employee_EndDate"]).ToString("dd/MM/yyyy");
                dgvEmployeeList.Rows[i].Cells["Position_Name"].Value = ds.Tables[0].Rows[i]["Position_Name"].ToString();
                //dgvEmployeeList.Rows[i].Cells["Branch_Name"].Value = ds.Tables[0].Rows[i]["Branch_Name"].ToString();
                dgvEmployeeList.Rows[i].Cells["Department_Name"].Value = ds.Tables[0].Rows[i]["Department_Name"].ToString();
            }
        }



        private void btnEmployee_Contract_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                employeeID = Convert.ToInt32(dgvEmployeeList.SelectedRows[0].Cells["Employee_ID"].Value);
                Form f = new frmLabourContractInfo(0, employeeID, listRole);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
            else
            {
                MessageBox.Show(UpdateUI.GetString("chuachonnhanvien"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        private void mnuSoanHDLD_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    employeeID = Convert.ToInt32(dgvEmployeeList.CurrentRow.Cells["Employee_ID"].Value);
                    frmPreviewReport f = new frmPreviewReport("ContractTemplate", new string[] { employeeID.ToString() });
                    f.Show(this);
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void mnuQDTangLuong_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    employeeID = Convert.ToInt32(dgvEmployeeList.SelectedRows[0].Cells["Employee_ID"].Value);
                    frmPreviewReport f = new frmPreviewReport("QDTangLuong", new string[] { employeeID.ToString() });
                    f.Show(this);
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }



        private void mnuCurriculumVitae_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                employeeID = Convert.ToInt32(dgvEmployeeList.SelectedRows[0].Cells["Employee_ID"].Value);
                if (listRole.Update || listRole.View)
                {
                    frmPreviewReport f = new frmPreviewReport("SYLL", new string[] { employeeID.ToString() });
                    f.Show(this);
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void frmEmployeeNotActiveList_Activated(object sender, EventArgs e)
        {
            //LoadLanguage();
            if (dgvEmployeeList.Columns.Count > 0)
            {
                dgvEmployeeList.Columns["RowNumber"].HeaderText = UpdateUI.GetString("stt");
                dgvEmployeeList.Columns["Employee_Code"].HeaderText = UpdateUI.GetString("maso");
                dgvEmployeeList.Columns["Employee_FullName"].HeaderText = UpdateUI.GetString("hovaten");
                dgvEmployeeList.Columns["Employee_DateOfBirth"].HeaderText = UpdateUI.GetString("ngaysinh");
                dgvEmployeeList.Columns["Employee_PlaceOfBirth"].HeaderText = UpdateUI.GetString("noisinh");
                dgvEmployeeList.Columns["Employee_Address"].HeaderText = UpdateUI.GetString("diachi");
                dgvEmployeeList.Columns["Employee_Phone"].HeaderText = UpdateUI.GetString("dienthoai");
                dgvEmployeeList.Columns["Employee_Sex"].HeaderText = "GT";
                dgvEmployeeList.Columns["MaritalStatus_Name"].HeaderText = UpdateUI.GetString("tinhtranghonnhan");
                dgvEmployeeList.Columns["Religion_Name"].HeaderText = UpdateUI.GetString("tongiao");
                dgvEmployeeList.Columns["Ethnic_Name"].HeaderText = UpdateUI.GetString("dantoc");
                dgvEmployeeList.Columns["Employee_IDCardNo"].HeaderText = UpdateUI.GetString("CMTND");
                dgvEmployeeList.Columns["Employee_IDCardIssueDate"].HeaderText = UpdateUI.GetString("ngaycap");
                dgvEmployeeList.Columns["Employee_IDCardIssueAuthority"].HeaderText = UpdateUI.GetString("noicap");
                dgvEmployeeList.Columns["Employee_BankAccountNo"].HeaderText = UpdateUI.GetString("sotaikhoan");
                //dgvEmployeeList.Columns["Employee_BankName"].HeaderText = UpdateUI.GetString("nganhang");
                dgvEmployeeList.Columns["Employee_StartDate"].HeaderText = UpdateUI.GetString("ngaybatdau");
                dgvEmployeeList.Columns["Employee_EndDate"].HeaderText = UpdateUI.GetString("ngayketthuc");
            }
        }



        private void frmEmployeeNotActiveList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfEmployee.Close();
            wcfBranch.Close();
            wcfDepartment.Close();
            wcfMain.Close();
        }


        private void mnuPrintSalaryDepartmentOne_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                employeeID = Convert.ToInt32(dgvEmployeeList.SelectedRows[0].Cells["Employee_ID"].Value);
                if (listRole.Update || listRole.View)
                {
                    frmPreviewReport f = new frmPreviewReport("TangLuong_ThuyenChuyen_1", new string[] { employeeID.ToString(), "false" });
                    f.Show(this);
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void mnuPrintSalaryDepartmentAll_Click(object sender, EventArgs e)
        {
            if (listRole.Update || listRole.View)
            {
                frmPreviewReport f = new frmPreviewReport("TangLuong_ThuyenChuyen_ALL", new string[] { "" });
                f.Show(this);
            }
            else
                MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);

        }



        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {

                    employeeID = Convert.ToInt32(dgvEmployeeList.SelectedRows[0].Cells["Employee_ID"].Value);
                    Form f = new frmEmployeeInfo(employeeID, 0, 0, UIParamater.Employee_NotActive, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        LoadData();
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                employeeID = Convert.ToInt32(dgvEmployeeList.SelectedRows[0].Cells["Employee_ID"].Value);

                //Kiểm tra nếu số sổ đã có phát sinh trong table HRM_BHDetail thì không cho xóa
                if (_wcfDetail.Detail_CheckExistSIBookNo(Convert.ToString(dgvEmployeeList.SelectedRows[0].Cells["Employee_SIBookno"].Value), Program.strBranchCode))
                {
                    MessageBox.Show("Nhân sự này đã đóng BHXH, hệ thống không cho phép xóa để theo dõi số liệu!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (wcfEmployee.Employee_Delete(employeeID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        LoadData();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnEditPopUp_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void btnDeletePopUp_Click(object sender, EventArgs e)
        {
            btnDelete_Click(null, null);
        }

        private void mnuIncreaseSalary_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    employeeID = Convert.ToInt32(dgvEmployeeList.CurrentRow.Cells["Employee_ID"].Value);
                    frmPositionDetailsInfo f = new frmPositionDetailsInfo(0, employeeID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                        LoadData();
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void mnuFurlough_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    employeeID = Convert.ToInt32(dgvEmployeeList.CurrentRow.Cells["Employee_ID"].Value);
                    frmLeaveWorkInfo f = new frmLeaveWorkInfo(0, employeeID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                        LoadData();
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void mnuOffered_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    employeeID = Convert.ToInt32(dgvEmployeeList.SelectedRows[0].Cells["Employee_ID"].Value);
                    RoleRightInfo _lRole = UIParamater.Quyen_DeNghiTroCap;

                    frmOfferedInfo f = new frmOfferedInfo(0, _lRole, employeeID);
                    if (f.ShowDialog(this) == DialogResult.OK)
                        LoadData();
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void mnuComment_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    employeeID = Convert.ToInt32(dgvEmployeeList.SelectedRows[0].Cells["Employee_ID"].Value);
                    RoleRightInfo _lRole = UIParamater.Quyen_HoSoCanBo;

                    frmCommentInfo f = new frmCommentInfo(0, employeeID, _lRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                        LoadData();
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void mnuQDNghiHuongBHXH_Click(object sender, EventArgs e)
        {

            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                if (listRole.Update || listRole.View)
                {
                    employeeID = Convert.ToInt32(dgvEmployeeList.CurrentRow.Cells["Employee_ID"].Value);
                    frmPreviewReport f = new frmPreviewReport("QDNghiHuongBHXH", new string[] { employeeID.ToString() });
                    f.Show(DockPanel);
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void mnuDeclaration_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                employeeID = Convert.ToInt32(dgvEmployeeList.SelectedRows[0].Cells["Employee_ID"].Value);
                string _SIBookNo = dgvEmployeeList.SelectedRows[0].Cells["Employee_SIBookNo"].Value.ToString();
                if (listRole.Update || listRole.View)
                {
                    if (employeeID > 0)
                    {
                        SalaryFundClient _wcfSalaryFund = Service.SalaryFund();
                        DataTable dtFund = _wcfSalaryFund.SalaryFund_GetDate(Program.strBranchCode).Tables[0];
                        DateTime _DateV = DateTime.MinValue;
                        if (dtFund.Rows.Count > 0)
                            _DateV = Convert.ToDateTime(dtFund.Rows[0]["SALARYFUND_MONTH"].ToString());
                        else
                            _DateV = DateTime.Now.Date;
                        frmQuaTrinhBHXH f = new frmQuaTrinhBHXH(employeeID, _SIBookNo, _DateV,0);
                        f.ShowDialog(this);
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void mnuPrintSalaryDepartment_Click(object sender, EventArgs e)
        {

        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                if (Program.isAdmin || Program.isViewAll)
                    branch_ID = 0;
                else
                    branch_ID = Program.intBranchID;
                department_ID = 0;
                LoadData();
            }
            else if (e.Node.Level == 1)
            {
                branch_ID = Convert.ToInt32(e.Node.Tag);
                department_ID = 0;
                currPage = 0;
                LoadData();
            }
            else if (e.Node.Level >= 2)
            {
                department_ID = Convert.ToInt32(e.Node.Tag);
                branch_ID = Convert.ToInt32(e.Node.Parent.Tag);
                currPage = 0;
                LoadData();
            }
        }

        private void dgvEmployeeList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            if (e.RowIndex >= 0)
                btnEdit_Click(null, null);
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void frmEmployeeNotActiveList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
            }

        }

        private void dgvEmployeeList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (dgvEmployeeList.Rows.Count > 0)
                {
                    mnuDeclaration.Enabled = true;
                    btnEditPopUp.Enabled = true;
                    btnDeletePopUp.Enabled = true;
                }
                else
                {
                    mnuDeclaration.Enabled = false;
                    btnEditPopUp.Enabled = false;
                    btnDeletePopUp.Enabled = false;
                }
            }
        }

        private void dgvEmployeeList_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            _OrderBy = dgvEmployeeList.SortedColumn.Name;
            switch (dgvEmployeeList.SortOrder.ToString())
            {
                case "Ascending":
                    _OrderDirection = "ASC";
                    break;
                case "Descending":
                    _OrderDirection = "DESC";
                    break;
            }
            LoadData();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            string _fileName = "";
            string branchName = "";
            string departmentName = "";
            string _Cre = "";
            DataTable dt = new DataTable();
            _fileName = "tmp.xls";
            if (branch_ID > 0)
            {
                branchName = wcfBranch.Branch_GetInfo(branch_ID).Name;
            }

            if (department_ID > 0)
            {
                departmentName = wcfDepartment.Department_GetInfo(department_ID).Name;
            }
            frmFieldExcel f = new frmFieldExcel(listRole, false);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                _Cre = f.Filed;
                string[] _ArrayCre = null;
                _ArrayCre = _Cre.Split(new char[] { ',' });
                dt = datasetForExcel.Tables[0].DefaultView.ToTable(true, _ArrayCre);
                try
                {
                    if (dt.Rows.Count > 0)
                    {
                        ExportData2Excel("DANH SÁCH NHÂN VIÊN THÔI VIỆC", dt, Path.GetTempPath() + "\\" + _fileName);
                        try
                        {
                            System.Diagnostics.Process.Start(Path.GetTempPath() + "\\" + _fileName);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Không mở được File \n" + Path.GetTempPath() + "\\" + _fileName, "Thông báo");
                        }
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("File " + Path.GetTempPath() + "\\" + _fileName + " đang được mở bằng một ứng dụng khác.\nHãy chọn file khác!", "Thông báo");
                }
            }
        }
        private void ExportData2Excel(string _Title, DataTable dt, string _FileName)
        {
            if (dt.Columns.Contains("RowNumber"))
                dt.Columns["RowNumber"].Caption = "STT";

            if (dt.Columns.Contains("Employee_LastName"))
                dt.Columns["Employee_LastName"].Caption = "Họ và tên đệm";

            if (dt.Columns.Contains("Employee_FirstName"))
                dt.Columns["Employee_FirstName"].Caption = "Tên";

            if (dt.Columns.Contains("Employee_FullName"))
                dt.Columns["Employee_FullName"].Caption = "Họ và tên";

            if (dt.Columns.Contains("Employee_DateOfBirth"))
                dt.Columns["Employee_DateOfBirth"].Caption = "Ngày sinh";

            if (dt.Columns.Contains("Employee_Sex"))
                dt.Columns["Employee_Sex"].Caption = "Giới tính";

            if (dt.Columns.Contains("Employee_PlaceOfBirth"))
                dt.Columns["Employee_PlaceOfBirth"].Caption = "Nơi sinh";

            if (dt.Columns.Contains("Employee_Type"))
                dt.Columns["Employee_Type"].Caption = "Loại nhân viên";

            if (dt.Columns.Contains("Employee_Code"))
                dt.Columns["Employee_Code"].Caption = "Mã NV";

            if (dt.Columns.Contains("Standard_Name"))
                dt.Columns["Standard_Name"].Caption = "Trình độ học vấn";

            if (dt.Columns.Contains("Employee_Phone"))
                dt.Columns["Employee_Phone"].Caption = "ĐT";

            if (dt.Columns.Contains("PD_BasicSalary"))
                dt.Columns["PD_BasicSalary"].Caption = "Lương cơ bản";

            if (dt.Columns.Contains("PD_TotalSalary"))
                dt.Columns["PD_TotalSalary"].Caption = "Lương thực nhận";

            if (dt.Columns.Contains("Employee_MedicalNo"))
                dt.Columns["Employee_MedicalNo"].Caption = "Số thẻ BHYT";

            if (dt.Columns.Contains("Employee_Description"))
                dt.Columns["Employee_Description"].Caption = "Ghi chú";

            if (dt.Columns.Contains("GradeType_Name"))
                dt.Columns["GradeType_Name"].Caption = "Loại công chức";

            if (dt.Columns.Contains("Position_Name"))
                dt.Columns["Position_Name"].Caption = "Chức vụ";

            if (dt.Columns.Contains("Grade_Name"))
                dt.Columns["Grade_Name"].Caption = "Ngạch/Chức danh";

            if (dt.Columns.Contains("ContractType_Name"))
                dt.Columns["ContractType_Name"].Caption = "Loại HĐ";

            if (dt.Columns.Contains("National_Name"))
                dt.Columns["National_Name"].Caption = "Quốc tịch";

            if (dt.Columns.Contains("LC_No"))
                dt.Columns["LC_No"].Caption = "Số HĐ hiện tại";

            if (dt.Columns.Contains("LC_Date"))
                dt.Columns["LC_Date"].Caption = "Ngày ký HĐ hiện tại";

            if (dt.Columns.Contains("LC_FinishDate"))
                dt.Columns["LC_FinishDate"].Caption = "Ngày hết hạn HĐ";

            if (dt.Columns.Contains("MaritalStatus_Name"))
                dt.Columns["MaritalStatus_Name"].Caption = "Tình trạng hôn nhân";

            if (dt.Columns.Contains("Religion_Name"))
                dt.Columns["Religion_Name"].Caption = "Tôn giáo";

            if (dt.Columns.Contains("Ethnic_Name"))
                dt.Columns["Ethnic_Name"].Caption = "Dân tộc";

            if (dt.Columns.Contains("Employee_Culture"))
                dt.Columns["Employee_Culture"].Caption = "Trình độ văn hóa";

            if (dt.Columns.Contains("Employee_BankAccountNo"))
                dt.Columns["Employee_BankAccountNo"].Caption = "Số TK ngân hàng";

            if (dt.Columns.Contains("Employee_BankName"))
                dt.Columns["Employee_BankName"].Caption = "Ngân hàng";

            if (dt.Columns.Contains("Employee_Specialist"))
                dt.Columns["Employee_Specialist"].Caption = "Chuyên ngành";

            if (dt.Columns.Contains("PD_Job"))
                dt.Columns["PD_Job"].Caption = "Nghề nghiệp";

            if (dt.Columns.Contains("Branch_Name"))
                dt.Columns["Branch_Name"].Caption = "Chi nhánh";

            if (dt.Columns.Contains("Employee_StartDate"))
                dt.Columns["Employee_StartDate"].Caption = "Ngày vào Cty";

            if (dt.Columns.Contains("Employee_IDCardNo"))
                dt.Columns["Employee_IDCardNo"].Caption = "Số CMT";

            if (dt.Columns.Contains("Employee_IDCardIssueDate"))
                dt.Columns["Employee_IDCardIssueDate"].Caption = "Ngày cấp";

            if (dt.Columns.Contains("Employee_IDCardIssueAuthority"))
                dt.Columns["Employee_IDCardIssueAuthority"].Caption = "Nơi cấp";

            if (dt.Columns.Contains("Employee_SIBookNo"))
                dt.Columns["Employee_SIBookNo"].Caption = "Số sổ BH";

            if (dt.Columns.Contains("Employee_SIBookIssueDate"))
                dt.Columns["Employee_SIBookIssueDate"].Caption = "Ngày cấp";

            if (dt.Columns.Contains("Employee_SIBookIssueAuthority"))
                dt.Columns["Employee_SIBookIssueAuthority"].Caption = "Nơi cấp";

            if (dt.Columns.Contains("Employee_MedicalCard"))
                dt.Columns["Employee_MedicalCard"].Caption = "Số thẻ BHYT";

            if (dt.Columns.Contains("Employee_MedicalDate"))
                dt.Columns["Employee_MedicalDate"].Caption = "Ngày cấp";

            if (dt.Columns.Contains("Employee_MedicalPlace"))
                dt.Columns["Employee_MedicalPlace"].Caption = "Nơi khám BHYT";

            if (dt.Columns.Contains("Employee_Address"))
                dt.Columns["Employee_Address"].Caption = "Địa chỉ";

            if (dt.Columns.Contains("Department_Name"))
                dt.Columns["Department_Name"].Caption = "Đơn vị";

            if (dt.Columns.Contains("Employee_EndDate"))
                dt.Columns["Employee_EndDate"].Caption = "Ngày thôi việc";

            if (dt.Columns.Contains("TypeOfLeaveWork_Name"))
                dt.Columns["TypeOfLeaveWork_Name"].Caption = "Hình thức thôi việc";

            ReadExcelFile.exportToExcel(_Title, dt, _FileName);
        }

        private void quáTrìnhĐóngBHTNToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvEmployeeList.SelectedRows.Count > 0)
            {
                employeeID = Convert.ToInt32(dgvEmployeeList.SelectedRows[0].Cells["Employee_ID"].Value);
                string _SIBookNo = dgvEmployeeList.SelectedRows[0].Cells["Employee_SIBookNo"].Value.ToString();
                if (listRole.Update || listRole.View)
                {
                    if (employeeID > 0)
                    {
                        SalaryFundClient _wcfSalaryFund = Service.SalaryFund();
                        DataTable dtFund = _wcfSalaryFund.SalaryFund_GetDate(Program.strBranchCode).Tables[0];
                        DateTime _DateV = DateTime.MinValue;
                        if (dtFund.Rows.Count > 0)
                            _DateV = Convert.ToDateTime(dtFund.Rows[0]["SALARYFUND_MONTH"].ToString());
                        else
                            _DateV = DateTime.Now.Date;
                        frmQuaTrinhBHXH f = new frmQuaTrinhBHXH(employeeID, _SIBookNo, _DateV,1);
                        f.ShowDialog(this);
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


    }
}

